<?php

namespace App\Entity;

use App\Enum\LogTypeEnum;
use App\Utils\Helper\LogHelper;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity(repositoryClass="App\Repository\AdminUserGroupRepository")
 * @ORM\HasLifecycleCallbacks()
 * @UniqueEntity("name")
 */
class AdminUserGroup {

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $module_ids;

    /**
     * @ORM\Column(type="boolean", nullable=true, options={"default"="0","comment"="是否超管 0否 1是"})
     */
    private $is_super = false;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $created_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $updated_at;

    public $module;

    public function __toString () {
        return $this->name;
    }

    public function getId (): ?int {
        return $this->id;
    }

    public function getName (): ?string {
        return $this->name;
    }

    public function setName (string $name): self {
        $this->name = $name;

        return $this;
    }

    public function getModuleIds (): ?string {
        return $this->module_ids;
    }

    public function setModuleIds (?string $module_ids): self {
        $this->module_ids = $module_ids;

        return $this;
    }

    public function getCreatedAt (): ?\DateTimeInterface {
        return $this->created_at;
    }

    public function setCreatedAt (?\DateTimeInterface $created_at): self {
        $this->created_at = $created_at;

        return $this;
    }

    public function getUpdatedAt (): ?\DateTimeInterface {
        return $this->updated_at;
    }

    public function setUpdatedAt (?\DateTimeInterface $updated_at): self {
        $this->updated_at = $updated_at;

        return $this;
    }

    public function getIsSuper (): ?bool {
        return $this->is_super;
    }

    public function setIsSuper (?bool $is_super): self {
        $this->is_super = $is_super;

        return $this;
    }

    /**
     * 新建之后 添加日志
     * @ORM\PostPersist()
     */
    public function buildLogByCreate () {
        $description = '用户组名:['.$this->getName().']';
        LogHelper::buildLog(LogTypeEnum::TYPE_ADMIN_USER_GROUP_CRATE, $description);
    }

    /**
     * 更新之后 添加日志
     * @ORM\PostUpdate()
     */
    public function buildLogByUpdate () {
        $description = '用户组名:['.$this->getName().']';
        LogHelper::buildLog(LogTypeEnum::TYPE_ADMIN_USER_GROUP_UPDATE, $description, $this);
    }

    /**
     * 删除之后 添加日志
     * @ORM\PreRemove()
     */
    public function buildLogByDelete () {
        $description = '用户组名:['.$this->getName().']';
        LogHelper::buildLog(LogTypeEnum::TYPE_ADMIN_USER_GROUP_DELETE, $description);
    }
}
